<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="颜色代码" prop="colorCode">
        <el-input
          v-model="queryParams.colorCode"
          placeholder="请输入颜色代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色名称" prop="colorName">
        <el-input
          v-model="queryParams.colorName"
          placeholder="请输入颜色名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="colorFlag">
        <el-select
          v-model="queryParams.colorFlag"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in colorFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:information:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:information:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:information:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['base:information:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="informationList"
      stripe
      border
      highlight-current-row
      height="calc(100% - 100px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="颜色代码" align="center" prop="colorCode" />
      <el-table-column label="颜色名称" align="center" prop="colorName" />
      <el-table-column label="A" align="center" prop="colorA" />
      <el-table-column label="B" align="center" prop="colorB" />
      <el-table-column label="L" align="center" prop="colorL" />
      <el-table-column label="△A" align="center" prop="colorVa" />
      <el-table-column label="△B" align="center" prop="colorVb" />
      <el-table-column label="△E" align="center" prop="colorVe" />
      <el-table-column label="状态" align="center" key="colorFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.colorFlag"
            active-value="0"
            inactive-value="1"
            @change="handleInfFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:information:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:information:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改颜色资料对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="颜色代码" prop="colorCode">
          <el-input v-model="form.colorCode" placeholder="请输入颜色代码" />
        </el-form-item>
        <el-form-item label="颜色名称" prop="colorName">
          <el-input v-model="form.colorName" placeholder="请输入颜色名称" />
        </el-form-item>
        <el-form-item label="A" prop="colorA">
          <el-input v-model="form.colorA" placeholder="请输入A" />
        </el-form-item>
        <el-form-item label="B" prop="colorB">
          <el-input v-model="form.colorB" placeholder="请输入B" />
        </el-form-item>
        <el-form-item label="L" prop="colorL">
          <el-input v-model="form.colorL" placeholder="请输入L" />
        </el-form-item>
        <el-form-item label="△A" prop="colorVa">
          <el-input v-model="form.colorVa" placeholder="请输入△A" />
        </el-form-item>
        <el-form-item label="△B" prop="colorVb">
          <el-input v-model="form.colorVb" placeholder="请输入△B" />
        </el-form-item>
        <el-form-item label="△E" prop="colorVe">
          <el-input v-model="form.colorVe" placeholder="请输入△E" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.colorFlag">
            <el-radio
              v-for="dict in colorFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation,
  exportInformation,
  changeInfFlag,
} from "@/api/base/information";

export default {
  name: "Information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 颜色资料表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      colorFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        colorCode: null,
        colorName: null,
        colorFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        colorCode: [
          { required: true, message: "颜色代码不能为空", trigger: "blur" },
        ],
        colorName: [
          { required: true, message: "颜色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_flag").then((response) => {
      this.colorFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询颜色资料列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then((response) => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleInfFlag(row) {
      let text = row.colorFlag === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changeInfFlag(row.colorId, row.colorFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.colorFlag = row.colorFlag === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        colorId: null,
        colorCode: null,
        colorName: null,
        colorA: null,
        colorB: null,
        colorL: null,
        colorVa: null,
        colorVb: null,
        colorVe: null,
        colorFlag: 0,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.colorId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加颜色资料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const colorId = row.colorId || this.ids;
      getInformation(colorId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改颜色资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.colorId != null) {
            updateInformation(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const colorIds = row.colorId || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delInformation(colorIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有颜色资料数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInformation(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
